﻿Namespace EIBData.EIS
    Public NotInheritable Class EIBDataEIS_9
        Inherits EIBDataEIS_Base

        Protected _value As Double = Nothing


        Sub New(data As Byte())
            If Not CanEncode(data) Then Throw New EIB.Exceptions.EIBDataDecodeException("")

            MyBase._data = data

            Dim s As Boolean = (data(1) And &H80) >> 7
            Dim e As Byte = ((data(1) And &H7F) << 1) + ((data(2) And &H80) >> 7)
            Dim m As UInt32 = ((data(2) And &H7F) << 16) + (data(3) << 8) + data(4)

            _value = -1 ^ s * m * 2 ^ e
        End Sub

        Sub New(value As Double)
            'If value < -671088.64 Then Throw New EIB.Exceptions.EIBDataEncodeException("")
            'If value > 670760.96 Then Throw New EIB.Exceptions.EIBDataEncodeException("")

            _value = value

            Dim s As Boolean = If(_value < 0, True, False)
            Dim e As Byte = 0
            Dim m As UInt32 = 0

            MyBase._data = New Byte() {0, (If(s, 1, 0) << 7) + ((e And &H7F) >> 1), ((e And &H1) << 7) + ((m And &H7F0000) >> 16), ((m And &HFF00) >> 8), (m And &HFF)}
        End Sub


        Public Overrides Function CanEncode(data() As Byte) As Boolean
            Return data IsNot Nothing AndAlso data.Length = EIS_OctetLength
        End Function

        Public Overrides Function ToString() As String
            Return _value
        End Function

        Public Overrides Function ToObject() As Object()
            Return New Object() {_value}
        End Function

        Public Overrides ReadOnly Property ObjectType As System.Type()
            Get
                Return New Type() {GetType(Double)}
            End Get
        End Property

        Public Overrides Function ToInfoList() As List(Of EIB.InfoBase)
            Dim list As New List(Of EIB.InfoBase)

            list.Add(New EIB.Info("Type", EIS_Name))
            list.Add(New EIB.Info("Value", _value))

            Return list
        End Function

        Public Shared Shadows ReadOnly Property EIS_OctetLength As Byte
            Get
                Return 5
            End Get
        End Property

        Public Shared Shadows ReadOnly Property EIS_Name As String
            Get
                Return "EIS 9"
            End Get
        End Property


        Public ReadOnly Property Value As Double
            Get
                Return _value
            End Get
        End Property
    End Class
End Namespace